package com.cms.util;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;

/**
 * 
 * @author zxpc
 *
 */
public class HttpUtils {

	/**
	 * get方式调用接口
	 * @param configKey 配置文件的key值
	 * @param parameter 调用接口参数map
	 * @return
	 */
	public static String getService(String configKey,Map<String,String> parameter) {
		
		//获取配置文件
		Prop p = PropKit.use("config.properties");
		String province = p.get(configKey);
		System.out.println("提交省本级的接口："+province);
		try {
			String test = HttpKit.get(province,parameter);
		    return "调用成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "调用失败";
		}
		
	}

	/**
	 * post请求接口 请求参数放在body json字符串格式
	 * @param url
	 * @param body
	 * @return
	 * @throws Exception
	 */
   public static String sendHttpPost(String url, String body) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
		httpPost.setEntity(new StringEntity(body,"UTF-8"));
		CloseableHttpResponse response = httpClient.execute(httpPost);
		System.out.println(response.getStatusLine().getStatusCode() + "\n");
		HttpEntity entity = response.getEntity();
		String responseContent = EntityUtils.toString(entity, "UTF-8"); 
		System.out.println("请求地址 "+url+"请求参数"+body+"结果"+responseContent);
		response.close();
		httpClient.close();
		return responseContent;

	}

	/**
	 *
	 * @param url
	 * @param parameter
	 * @param city
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
   public static Integer fun5(String url,String parameter,String city) throws ClientProtocolException, IOException{
		//1、创建HttpClient
		org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
		
		//2、设置超时时间 10s
		httpClient.setConnectionTimeout(10000);
		
		
		//3、创建get或post请求方法
		PostMethod method = new PostMethod(url);
		//4、设置编码
		httpClient.getParams().setContentCharset("UTF-8");
		//5、设置请求消息头，为表单方式提交
		method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		
		//6、设置参数
		method.setParameter("categoryId", parameter);
		method.setParameter("city", city);
		//method.setParameter("password", "123456");
		
//		7、执行提交
		httpClient.executeMethod(method);
		//System.out.println(method.getStatusLine());
		
		//System.out.println(method.getResponseBodyAsString());
		
		return method.getStatusCode();
		
	}
   
	
	
	
   
}

